//
//  JokeViewController.m
//  Days
//
//  Created by lanou3g on 16/1/6.
//  Copyright © 2016年 dss. All rights reserved.
//

#import "JokeViewController.h"
#import "JokeView.h"
#import "JokeTableViewCell.h"
#import "DataHandle.h"
#import "Model.h"
@interface JokeViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)JokeView *jokeView;

@property(nonatomic,strong)NSArray *array;

@end

@implementation JokeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationItem.title = @"开心一笑";
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"3.jpeg"]];
    imageView.frame = CGRectMake(0, 0, self.view.frame.size.width-170, self.view.frame.size.height+100);
    [self.view addSubview:imageView];
    
    self.jokeView = [[JokeView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.jokeView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.jokeView];
    
    
    //回调刷新
    [[DataHandle sharedDataHandle]getDataFromNet:^(id isFinished) {
        self.array = isFinished;
        NSLog(@"******%@*********",self.array);
        [self.jokeView.tableView reloadData];
    }];
    
    //设置代理
    self.jokeView.tableView.delegate = self;
    self.jokeView.tableView.dataSource = self;
    
    //注册xib
    [self.jokeView.tableView registerNib:[UINib nibWithNibName:@"JokeTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"JokeTableViewCell"];
    
}

#pragma mark ---实现代理方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 1;

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [DataHandle sharedDataHandle].DataArray.count;
   

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    JokeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JokeTableViewCell"];
    cell.backgroundColor = [UIColor clearColor];
    Model *model = self.array[indexPath.row];
    cell.updateLabel.text = model.updatetime;
    cell.contentLabel.text = model.content;
    
    //获取到cell.label.text的自适应高度
    CGFloat height = [JokeTableViewCell heightForLabelText:cell.contentLabel.text];
    //修改cell.label的高度
    CGRect rect = cell.contentLabel.frame;
    rect.size.height = height;
    cell.contentLabel.frame = rect;
    
    return cell;


}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    Model *model = self.array[indexPath.row];
    CGFloat height = [JokeTableViewCell heightForLabelText:model.content];
    return height+50;
    

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];


}


@end
